package ccgl.com.util;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

public class GsonPrint {
	/**
	 * @author 邓洪书 主要向前台输出Gson数据
	 * @param response 
	 * @throws IOException 
	 */
	public static void printGson(Object obj, HttpServletResponse response) throws IOException {
		Gson gson = new Gson();
		String result = gson.toJson(obj);
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		
		PrintWriter out = null;
		out = response.getWriter();
		out.print(result);
		out.flush();
		out.close();
	}
	
	/**
	 * 输出"{'data':'" + data + "'}"格式的json数据
	 * @param data 提示信息
	 * @param response
	 * @throws IOException
	 */
	public static void printFormatGson(String data, HttpServletResponse response) throws IOException {
		String result = "{\"data\": \"" + data + "\"}";
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
		}
		out.print(result);
		out.flush();
		out.close();
	}
	
	/**
	 * @author 邓洪书 主要向前台输出数据
	 * charset 输出格式 "text/html; charset=utf-8"
	 */
	public static void printData(Object obj, String charset, HttpServletResponse response) {
		response.setContentType(charset);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
		}
		out.print(obj);
		out.flush();
		out.close();
	}

	/**
	 * @author 邓洪书 主要向前台输出数据
	 * @throws IOException 
	 */
	public static void printData(Object obj, HttpServletResponse response) throws IOException {
		response.setContentType("text/html; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = null;
		out = response.getWriter();
		out.print(obj);
		out.flush();
		out.close();
	}

}
